package net.mindview.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;

public class TextFile {
    private String filename;
    public TextFile(String name, String filename) {
        this.filename=filename;
    }

    /**
     *
     * @param fileName The full path of the target file
     * @return
     */

    public static String read(String fileName){

        StringBuilder sb = new StringBuilder();

        try {
            BufferedReader in = new BufferedReader(new FileReader(new File(fileName).getAbsoluteFile()));

            String s;
            try {
                while( (s = in.readLine()) != null ){
                    sb.append(s);
                    sb.append("\n");
                }
            }finally{
                in.close();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        return sb.toString();
    }

    /**
     *
     * @param fileName The full path of the target file
     * @param text The content which is written to the target file
     */
    public static void write(String fileName, String text){
        try {
            PrintWriter out = new PrintWriter(new File(fileName).getAbsoluteFile());
            out.print(text);
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String toString() {
        return read(filename);
    }
}